package org.eduapp.trains.service.account.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.eduapp.trains.DAOException;
import org.eduapp.trains.ServiceException;
import org.eduapp.trains.dao.account.AccountDAO;
import org.eduapp.trains.model.account.ModelAccount;
import org.eduapp.trains.service.account.AccountService;
import org.eduapp.trains.utils.UtilsString;
import org.eduapp.trains.web.support.PaginationSupport;
import org.hibernate.Hibernate;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;


/**
 * 
 * @author jeccy
 *
 */
public class AccountServiceImpl implements AccountService {
	
	private final static Logger logger = Logger.getLogger(AccountServiceImpl.class);
	
	private AccountDAO accountDAO;

	@Override
	public ModelAccount get(String id) throws ServiceException {
		if (UtilsString.isNotEmpty(id)) {
			try {
				return accountDAO.get(id);
			} catch (DAOException e) {
				logger.error("get account by id" + id + ",caused by:" + e.getMessage());
				e.printStackTrace();
			} catch (Exception e) {
				logger.error("get account by id" + id + ",caused by:" + e.getMessage());
				throw (new ServiceException(e));
			}
		} 
		return null;
	}
	
	@Override
	public ModelAccount getByNo(String accountNo) throws ServiceException {
		if (UtilsString.isNotEmpty(accountNo)) {
			try {
				return accountDAO.getAccountByNo(accountNo);
			} catch (DAOException e) {
				logger.error("get account by account no[" + accountNo + "],caused by:" + e.getMessage());
				e.printStackTrace();
			} catch (Exception e) {
				logger.error("get account by account no[" + accountNo + "],caused by:" + e.getMessage());
				throw (new ServiceException(e));
			}
		} 
		return null;
	}
	
	@Override
	public PaginationSupport<ModelAccount> get(ModelAccount model,
			int pageSize, int offset, String order, Boolean isDesc)
			throws ServiceException {
		try {
			return accountDAO.get(model, pageSize, offset, order, isDesc);
		} catch (DAOException e) {
			logger.error("get accounts by pager failed ,caused by:" + e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			logger.error("get accounts by pager failed ,caused by:" + e.getMessage());
			throw (new ServiceException(e));
		}
		return null;
	}

	@Override
	public List<ModelAccount> getByModel(ModelAccount model) throws ServiceException {
		try {
			return accountDAO.getByModel(model);
		} catch (DAOException e) {
			logger.error("get accounts by model failed ,caused by:" + e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			logger.error("get accounts by model failed ,caused by:" + e.getMessage());
			throw (new ServiceException(e));
		}
		return null;
	}

	@Override
	public boolean remove(String id) throws ServiceException {
		if (UtilsString.isNotEmpty(id)) {
			ModelAccount model = get(id);
			if (model != null) {
				try {
					accountDAO.remove(model);
					return true;
				} catch (DAOException e) {
					logger.error("Remove account failed with id" + id + ",caused by:" + e.getMessage());
					e.printStackTrace();
				} catch (Exception e) {
					logger.error("Remove account failed with id" + id + ",caused by:" + e.getMessage());
					throw (new ServiceException(e));
				}
			} else {
				logger.info("Remove account failed, id with " + id + "not found");
			}
		}
		logger.info("Remove account failed, empty id found");
		return false;
	}

	@Override
	public boolean saveOrUpdate(ModelAccount model) throws ServiceException {
		if (model != null) {
			try {
				accountDAO.saveOrUpdate(model);
				return true;
			} catch (DAOException e) {
				logger.error("SaveOrUpdate account failed with model" + model.toString() + ",caused by:" + e.getMessage());
				e.printStackTrace();
			} catch (Exception e) {
				logger.error("SaveOrUpdate account failed with model" + model.toString() + ",caused by:" + e.getMessage());
				throw (new ServiceException(e));
			}
		}
		logger.info("SaveOrUpdate account failed, empty model found");
		return false;
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public ModelAccount getAccountAuth(String username, String password){
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelAccount.class);
		criteria.add(Restrictions.eq("accountNo", username));
		criteria.add(Restrictions.eq("accountPassword", password));
		try {
			List<ModelAccount> result = (List<ModelAccount>)accountDAO.getListByCriteria(criteria);
			if(result != null && result.size() > 0){
				ModelAccount account=result.get(0);
				//Hibernate.initialize(user.getRoleModel().getMenus());
				//user.getRoleModel().getMenus();
				return account;
			}
			else return null;
		} catch (DAOException e) {
			logger.error(e);
			return null;
		} 
	}


	public void setAccountDAO(AccountDAO accountDAO) {
		this.accountDAO = accountDAO;
	}

	public AccountDAO getAccountDAO() {
		return accountDAO;
	}

	public static Logger getLogger() {
		return logger;
	}
}
